<?php

/**
 * @author TEAM-03HEDSPI
 * @copyright 2012
 */

class Upload{
   
    /**
     * @author Hienbv
	 * @global file ?nh d?i di?n
	 * Enter description here ...
     * @param unknown_type $_FILE
	 * @param unknown_type $USERNAME
     * @param unknown_type $name
     * @param unknown_type $root  
	 */
    function upload_file_news($_FILE,$username,$name,$root){
        $filename=$_FILES[$name]['name'];
        $type=substr(strrchr($filename, '.'), 1);
        if($type!='jpg'&& $type != 'gif' && $type != 'png' ){
                $result= 0;  // Khong dung dinh dang file  
        }
        
        $result="";	
        $upload_dir =$root.$username.'/'.date("d-m-y").'/';
        $uploadfile=$root.$username.'/'.date("d-m-y").'/'.basename($_FILE[$name]['name']);
        if(!is_dir($root.$username))
            mkdir($root.$username);
        if(!is_dir($root.$username."/".date('d-m-y'))){
            mkdir($root.$username."/".date("d-m-y"));
        }
        $file_rename= date('d-m-y-H-m-s');
        if (move_uploaded_file($_FILES[$name]['tmp_name'], $uploadfile)) {
            if (file_exists(($upload_dir.$_FILES[$name]['name'])))
						rename($upload_dir.$_FILES[$name]["name"],$upload_dir.$file_rename.'.'.$type);
            return $upload_dir.$file_rename.'.'.$type;        
        } else {
            $result=2;
        }
        return $result;
    }
}
?>